<?php

namespace App\Http\Controllers\Purchase;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Order\SaleorderController;
use App\Http\Validate\Refund\CustomRefund;
use App\Models\Balance\Balance;
use App\Models\Chanjet\TplusExpenseVoucher;
use App\Models\Custom\Custom;
use App\Models\Purchase\ExpenseVoucherInfoPay;
use App\Models\Purchase\ExpenseVoucherInfoPayApprove;
use App\Models\Receivemoney\Receipt;
use App\Models\Refund\CustomRefund as ModelCustomRefund;
use App\Models\Settle\SettleCompany;
use App\Services\Common\SnCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ExpenseVoucherInfoPayApproveController extends BaseController
{
    /***
     *  办款完成，给供应商余额加钱
     *
     *
     *
    */
    public function dopayment(Request $request){
        $params = ['id', 'bank_name', 'bk_amount', 'bk_url', 'bk_remark'];
        $req_data = $request->only($params);
        if(empty($req_data['id'])){
            return response()->json(['code' => '001', 'msg' => '付款id不存在']);
        }
        if(!isset($req_data['bk_amount'])){
            return response()->json(['code' => '001', 'msg' => '办款金额异常']);
        }
        if(empty($req_data['bk_url'])){
            return response()->json(['code' => '001', 'msg' => '办款回单不能为空']);
        }
        $ExpenseVoucherInfoPay = ExpenseVoucherInfoPay::where([
            'id' => $req_data['id']
        ])->first();
        if (!$ExpenseVoucherInfoPay) {
            return response()->json(['code' => '001', 'msg' => '付款记录不存在']);
        }
        //$ExpenseVoucherInfoPay = $ExpenseVoucherInfoPay->toArray();
        if(empty($req_data['bank_name'])){
            return response()->json(['code' => '001', 'msg' => '银行名称不能为空']);
        }


        $mangerinfo = $this->mangerinfo;
        $cpid = $mangerinfo['cpid'] ?? 0;
        $manger_id = $mangerinfo['id'] ?? 0;
        $realname = $mangerinfo['realname'];


        DB::beginTransaction();
        try{
            ExpenseVoucherInfoPay::where([
                'id' => $req_data['id']
            ])->update([
                'bk_status' => 2,
                //'bank_name' => $req_data['bank_name'],
                'bk_amount' => DB::raw('bk_amount + ' . SaleorderController::amountTransformIntoCent($req_data['bk_amount'])),
                //'bk_url' => $req_data['bk_url'],
                //'bk_remark' => $req_data['bk_remark'] ?? '',
            ]);
            //办款详情

            \App\Models\Purchase\ExpenseVoucherInfoPayBankuan::create([
                'cpid' => $cpid,
                'bank_name' => $req_data['bank_name'],
                'bk_amount' => SaleorderController::amountTransformIntoCent($req_data['bk_amount']),
                'bk_url'    => $req_data['bk_url'],
                'bk_remark' => $req_data['bk_remark'] ?? '',
                'main_id'   => $req_data['id'],
                'create_at' => time()
            ]);

            /****
             * 办款完成
             * 费用单加余额
             *
            */
            $expense_info = ExpenseVoucherInfoPay::with('expense_info')->where('id',$req_data['id'])->first();
            $expense_info = $expense_info['expense_info'];
            if(isset($expense_info) && $expense_info['main_id']){
                $main_id = $expense_info['main_id'];
                $company_code = TplusExpenseVoucher::where('id',$main_id)->value('company_code');
                $custom_id = Custom::where(['cpid'=>$cpid,'company_code'=>$company_code])->value('id');
                Balance::where([
                    'custom_id' => $custom_id
                ])->update([
                    'balance' => DB::raw('balance + ' . SaleorderController::amountTransformIntoCent($req_data['bk_amount'])),
                    'update_at' => time()
                ]);
            }
            DB::commit();
        }catch(\Exception $e){
            DB::rollBack();
            return response()->json(['code' => '001', 'msg' => '' . $e->getMessage()]);
        }
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
